<?php

/**
 * 后台广告管理模块 Controller
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: ad.php 31 2011-07-11 15:25:51Z zwt007 $
 * http://www.t-mac.org；
 */
class adAction extends Action
{

    private $tmp_model;

    /**
     * _init 方法 在执行任何Action前执行
     */
    public function _init()
    {
        $this->assign('action', $_GET['TMAC_ACTION']);
        $this->tmp_model = Tmac::model('ad');
        $check_model = $this->M('check');
        $check_model->checkLogin();
        $check_model->CheckPurview('tb_admin');
    }

    /**
     * 广告管理 首页
     */
    public function index()
    {
        //TODO  取出所有广告
        $rs = $this->tmp_model->getAdList();
        $this->V('ad');
    }

    /**
     * 新增/修改广告页面
     */
    public function add()
    {
        $ad_id = (int) $_GET['ad_id'];
        if ($ad_id > 0) {
            $editinfo = $this->tmp_model->getAdInfo($ad_id);
        }
        //取广告类型option数组
        $ad_type_radio_ary = UtilityConfig::ad_type_radio_ary();
        $ad_type_radio_option = Utility::RadioButton($ad_type_radio_ary, 'type_radio', $editinfo['ad_type_radio'], 'onclick="typechange(this.value);"');

        //取广告状态option数组
        $ad_state_radio_ary = UtilityConfig::ad_state_radio_ary();
        $ad_state_radio_option = Utility::RadioButton($ad_state_radio_ary, 'state_radio', $editinfo['ad_state_radio'], 'onclick="statechange(this.value);"');


        //初始化一下    默认state_radio
        $editinfo['ad_order'] = !empty($editinfo['ad_order']) ? $editinfo['ad_order'] : '0';
        $editinfo['ad_link'] = !empty($editinfo['ad_link']) ? $editinfo['ad_link'] : 'http://';
        $editinfo['ad_starttime'] = !empty($editinfo['ad_starttime']) ? date('Y-m-d', $editinfo['ad_starttime']) : '';
        $editinfo['ad_endtime'] = !empty($editinfo['ad_endtime']) ? date('Y-m-d', $editinfo['ad_endtime']) : '';

        $this->assign('ad_type_radio_option', $ad_type_radio_option);
        $this->assign('ad_state_radio_option', $ad_state_radio_option);
        $this->assign('editinfo', $editinfo);
        //TODO　载入资讯类别添加表单
        $this->V('ad');
    }

    /**
     * 广告管理 insert update => save()
     */
    public function save()
    {
        if (empty($_POST) || count($_POST) < 1) {
            $this->redirect('don\'t be evil');
            exit;
        }

        /* 初始化变量 */
        $ad_id = !empty($_POST['ad_id']) ? (int) $_POST['ad_id'] : 0;
        $area = !empty($_POST['area']) ? trim($this->H($_POST['area'])) : '';
        $title = !empty($_POST['title']) ? trim($this->H($_POST['title'])) : '';
        $sizewidth = !empty($_POST['sizewidth']) ? $_POST['sizewidth'] : '';
        $sizeheight = !empty($_POST['sizeheight']) ? $_POST['sizeheight'] : '';
        $link = !empty($_POST['link']) ? trim($this->H($_POST['link'])) : '';
        $ad_type_radio = !empty($_POST['type_radio']) ? $_POST['type_radio'] : '';
        $ad_uploadfile = !empty($_POST['thumb']) ? $_POST['thumb'] : '';
        $ad_externallinks = !empty($_POST['externallinks']) ? $_POST['externallinks'] : '';
        $ad_order = !empty($_POST['order']) && is_numeric($_POST['order']) ? $_POST['order'] : '0';
        $ad_state_radio = !empty($_POST['state_radio']) ? $_POST['state_radio'] : '';
        $ad_starttime = !empty($_POST['ad_starttime']) ? strtotime($_POST['ad_starttime']) : '';
        $ad_endtime = !empty($_POST['ad_endtime']) ? strtotime($_POST['ad_endtime']) : '';

        /* 验证不能为空 */
        if (empty($area)) {
            $this->redirect('广告位置不能为空!');
            exit;
        }
        if (empty($title)) {
            $this->redirect('广告标题不能为空');
            exit;
        }
        if (empty($link)) {
            $this->redirect('广告链接不能为空');
            exit;
        }
        // TODO goon to verify

        $info = array(
            'ad_title' => $title,
            'ad_width' => $sizewidth,
            'ad_height' => $sizeheight,
            'ad_link' => $link,
            'ad_type_radio' => $ad_type_radio,
            'ad_uploadfile' => $ad_uploadfile,
            'ad_order' => $ad_order,
            'ad_state_radio' => $ad_state_radio,
            'ad_starttime' => $ad_starttime,
            'ad_endtime' => $ad_endtime,
            'ad_area' => $area,
            'ad_addusername' => $_SESSION['admin_username'],
            'ad_externallinks' => $ad_externallinks,
            'ad_addtime' => time()
        );

        if ($ad_id > 0) {
            $ad_page = HttpResponse::getCookie('ad_page');
            //update save article
            $where = 'ad_id = ' . $ad_id . '';
            $rs = $this->tmp_model->addAd($info, 'UPDATE', $where);
            if ($rs) {
                $this->redirect('修改广告内容成功', PHP_SELF.'?m=ad&page= ' . $ad_page . '');
            } else {
                $this->redirect('修改广告内容失败');
            }
        } else {
            //insert save article_class
            $rs = $this->tmp_model->addAd($info);
            if ($rs) {
                $this->redirect('添加广告成功', PHP_SELF . '?m=ad');
            } else {
                $this->redirect('添加广告失败');
            }
        }
    }

}